<?php

namespace app\admin\controller\merchant;

class Refund extends Base
{
    protected $model = null;

    protected $searchFields = 'id|oid|refund_out_trade_no|O.sid|OR.uid';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = loader('admin')->loadModel('jy\\OrderRefund');
        $this->model->alias('OR');
    }

    public function index() {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $model = $this->model;
            $model->join('order O', 'OR.oid = O.id', 'left');
            $model->append(['status_text']);
            $model->where($where);
            $model->field('OR.*,O.sid');
            $model->order($sort, $order);
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $this->get_status_options();
        return $this->view->fetch();
    }

    public function product() {
        $this->request->filter(['strip_tags', 'trim']);
        $orid = $this->request->param('orid');
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->searchFields = 'ORP.id|SP.name|SP.store_name|SP.classify|SPS.name|SP.selling|SPS.price|SP.ost_sale';
            $this->model = $model = loadModel('jy\\OrderRefundProduct');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $model->alias('ORP');
            $model->join('order_product OP', 'ORP.opid = OP.id');
            $model->join('snapshot_product SP', 'OP.snapshotid = SP.id');
            $model->join('snapshot_product_specs SPS', 'SP.id = SPS.spid and OP.psid = SPS.psid');
            $model->field('ORP.id,ORP.num,ORP.createtime,ORP.updatetime,OP.num as buynum,SP.store_name,SP.store_photo,SP.photo,SP.name,SP.selling,SP.classify,SP.ost_sale,SPS.name as psname, SPS.price as price');
            $model->where('orid', $orid);
            $model->where($where);
            $model->order($sort, $order);
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);  
        }
        $this->assign('orid', $orid);
        return $this->view->fetch();
    }

    //操作记录
    public function operate_log() {
        $this->request->filter(['strip_tags', 'trim']);
        $orid = $this->request->param('orid');
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->searchFields = 'ORL.id|name|remark';
            $this->model = $model = loadModel('jy\\OrderRefundLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $model->alias('ORL');
            $model->append(['type_text', 'images_url']);
            $model->where('orid', $orid);
            $model->where($where);
            $model->order($sort, $order);
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);  
        }
        $this->assign('orid', $orid);
        $this->get_operate_type_options();
        return $this->view->fetch();
    }

    public function get_status_options() {
        $status_options = loadEnum('OrderRefundStatus')->get_options();
        $this->assign('status_options', (object)$status_options);
    }

    public function get_operate_type_options() {
        $operate_type_options = loadEnum('OrderRefundLogType')->get_options();
        $this->assign('operate_type_options', (object)$operate_type_options);
    }

    //申请退款
    public function agree() {
        if($this->request->isAjax()) {
            try {
                $arg = loadProcess('OrderRefund')->administrator_hanlde_all();
            } catch(\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
        $id = $this->request->param('id');
        $this->assign('id', $id);
        return $this->view->fetch();
    }

    //拒绝退款
    public function refuse() {
        if($this->request->isAjax()) {
            try {
                $arg = loadProcess('OrderRefund')->administrator_hanlde_all();
            } catch(\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
        $id = $this->request->param('id');
        $this->assign('id', $id);
        return $this->view->fetch();
    }
}
